/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.mer.entity.Attribute;
import com.kaaction.modules.mer.entity.MerClassify;
import com.kaaction.modules.mer.dao.AttributeDao;
import com.kaaction.modules.mer.dao.AttributeValueDao;

/**
 * 属性Service
 * @author huanghaiming
 * @version 2017-05-18
 */
@Service
@Transactional(readOnly = true)
public class AttributeService extends CrudService<AttributeDao, Attribute> {
	
	@Autowired
	AttributeValueDao attributeValueDao;
	
	public Attribute get(String id) {
		Attribute attribute = new Attribute();
		
		List<Attribute> list  = super.dao.findAttr(id);
		
		for(Attribute entity:list){
			//entity.getEventAttList();
		}
		
		logger.info("entity:" + attribute);
		
		return attribute;
	}
	
	public List<Attribute> findList(Attribute attribute) {
		return super.findList(attribute);
	}
	
	public Page<Attribute> findPage(Page<Attribute> page, Attribute attribute) {
		return super.findPage(page, attribute);
	}
	
	@Transactional(readOnly = false)
	public void save(MerClassify merClassify) {
		
		String classifyId = merClassify.getClassifyId();
		List<Attribute> list = merClassify.getEventAttList();
		logger.info("list:" + list);
		for(Attribute entity:list){
			entity.setClassifyId(classifyId);
			logger.info("entity:" + entity);
			logger.info("name:" + entity.getName());
			super.save(entity);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Attribute attribute) {
		super.delete(attribute);
	}

	public List<Attribute> getAttributeList(String classifyId) {
		return super.dao.findAttr(classifyId);
	}

	public List<Attribute> selectList(Attribute attribute) {
		return super.dao.selectList(attribute);
	}

	@Transactional(readOnly = false)
	public Map<String, Object> deleteAttr(Attribute attribute) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		int cnt = super.dao.deleteAttr(attribute);
		if(cnt>0){
			attributeValueDao.deleteAttrVal(attribute.getId());
			map.put("code", "0");
			map.put("msg", "删除成功");
		}else{
			map.put("code", "1");
			map.put("msg", "删除失败，请联系管理员");
		}
		return map;
	}

	public List<Map<String, String>> selectIds(Map<String, Object> map) {
		return super.dao.selectIds(map);
	}
	
}